package callnumber.gtdev5.com.photoVault.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.gtdev5.geetolsdk.mylibrary.util.ToastUtils;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import callnumber.gtdev5.com.photoVault.R;
import callnumber.gtdev5.com.photoVault.base.BaseActivity;
import callnumber.gtdev5.com.photoVault.bean.PasswordBean;

public class ChangePasswordActivity extends BaseActivity {
    @BindView(R.id.iv_dis)
    ImageView iv_dis;
    @BindView(R.id.old_Password)
    EditText oldPassword;
    @BindView(R.id.new_Password)
    EditText new_Password;
    @BindView(R.id.bt_Sure)
    Button btSure;
    List<PasswordBean> datas;

    @Override
    protected int getLayouId() {
        return R.layout.activity_change_password;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        ButterKnife.bind(this);
    }

    @Override
    protected void initDatas() {
        datas = new ArrayList<>();
        datas = DataSupport.findAll(PasswordBean.class);
        iv_dis.setOnClickListener(v -> finish());
        btSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(oldPassword.getText().toString())) {
                    ToastUtils.showShortToast("请输入原密码");
                    return;
                }
                if (TextUtils.isEmpty(new_Password.getText().toString())) {
                    ToastUtils.showShortToast("请输入新密码");
                    return;
                }
                if ((oldPassword.getText().toString().equals(datas.get(0).getPassword()))) {
                    DataSupport.deleteAll(PasswordBean.class);
                    PasswordBean passwordBean = new PasswordBean();
                    passwordBean.setPassword(new_Password.getText().toString() + "");
                    passwordBean.save();
                    ToastUtils.showShortToast("修改成功！");
                    finish();
                } else {
                    ToastUtils.showShortToast("密码错误！请重新输入！");
                }


            }
        });
    }

    @Override
    protected void initAction() {

    }

}
